package com.fix.beans;


import java.io.Serializable;
import java.io.UnsupportedEncodingException;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import com.fix.model.Guide;
import com.fix.util.FixClientController;
import com.google.gson.Gson;

@ManagedBean(name="guidebean")
@RequestScoped
public class GuideBean implements Serializable {
	private static final long serialVersionUID = 1L;	
	public Guide guide;
	
	public GuideBean() {
		System.out.println("param: " + getParamValue("guideid"));
		String guideId = getParamValue("guideid");
		findGuide(guideId);		
	}
	public Guide getGuide() {
		return guide;
	}
	public void setGuide(Guide guide) {
		this.guide = guide;
	}
	
	private void findGuide(String guideId) {
		final FixClientController fixClientController = new FixClientController();
		Gson gson = new Gson();
		String json = "";
		try {
			json = fixClientController.getGuide(Integer.valueOf(guideId));
			guide = gson.fromJson(json, Guide.class);
//			
//			System.out.println(guide.getTitle());
//			Step[] steps = guide.getSteps();
//			for (int j = 0; j < steps.length; j++) {
//				System.out.println("Step # " + steps[j].getOrderby());
//				Line[] lines = steps[j].getLines();
//				for (int k = 0; k < lines.length; k++) {
//					String text_raw = lines[k].getText_raw();
//					String jsonfromyadex = fixClientController.translate(text_raw);
//					Translated tr = gson.fromJson(jsonfromyadex, Translated.class);
//					//System.out.println("  " + lines[k].getLevel() + " " + tr.getText()[0] );
//				}
//				Media media = steps[j].getMedia();
//				Data[] data = media.getData();
//				for (int l = 0; l < data.length; l++) {
//					System.out.println("image: " + data[l].getStandard());
//				}
//			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	private String getParamValue(String param) {
		HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
		final String p = req.getParameter(param);
		return p;
	}
	
}
